#include "cv.h"
#include <highgui.h>
#include <iostream>

#include "handLocaliser.h"
#include "faceDetector.h"

using namespace std;
using namespace cv;


int main( int argc, const char** argv ) {

	//HandLocaliser* hl = new HandLocaliser();
	
	//TODO : Use resource tracker
	
	//FIX : Is this actually RGB, or might it be something else?
	
	//Mat image = imread( "../../samples/facetest1.jpg", 1 );
	
	Mat imgMat = imread( argv[1], 1 ); //TODO asking for bad input!

	//OK SO FAR
	
//	//TODO: Wow! This is really ugly, some functions work on Mat, others on IplImage
//	//Find C++ Equivalents?
//	//FIX : Migrate to proper C++
	
	FaceDetector* fd = new FaceDetector(1.0);

	vector<Rect>* faces = fd->detectFace(imgMat);
	
	if (!faces->empty()) {
		
		vector<Rect>::const_iterator faceRect = faces->begin();

////		cout << r->y << endl;
////		
////		Scalar color = CV_RGB(0,128,255);
////		
////		rectangle( image, cvPoint(r->x, r->y), cvPoint(r->x+r->width, r->y + r->height), color, 3);
////		
////		cv::imshow( "result", image );
////		
////		cvWaitKey(0);
////		
////		cvDestroyWindow("result");
		
		IplImage img = imgMat;
		
		//FIX This is getting really bad!
		

		int width     = img.width;

		int height    = img.height;

		int depth     = img.depth;

		int nchannels = img.nChannels;
		
		cout << width << " " << height << " " << depth << " " << nchannels << endl;

		/* create destination image */

		IplImage *imgcpy_Ptr = cvCreateImage( cvSize( width, height ),

									  depth, nchannels );
		
		cvCopy( &img, imgcpy_Ptr, NULL );
		
		cvSetImageROI( imgcpy_Ptr, cvRect(faceRect->x,faceRect->y,faceRect->width,faceRect->height) );
		
		IplImage* hsvFaceImage = HandLocaliser::createHSVImage( imgcpy_Ptr );
		
		
		//FIX : seems like image display is unstable, is there a bug with
		//deallocation of img, or something like that?
		
		//cvShowImage("hsv face image", hsvFaceImage);
		//		
		//cvWaitKey(0);
		
		IplImage *h_plane, *s_plane, *v_plane;
		IplImage *planes[2];
		
		cout << "got here. 1.1" << endl;
		
		//TODO: Do we have to assign v_plane?
		//Documentation seems to suggest it is necessary to have all.
		h_plane  = cvCreateImage( cvGetSize(hsvFaceImage), 8, 1 );
		s_plane  = cvCreateImage( cvGetSize(hsvFaceImage), 8, 1 );
		v_plane  = cvCreateImage( cvGetSize(hsvFaceImage), 8, 1 );
		planes[0] = h_plane;
		planes[1] = s_plane;
		cvCvtPixToPlane( hsvFaceImage, h_plane, s_plane, v_plane, 0 );
		
		CvHistogram* hist = HandLocaliser::computeHistogram( planes );
		
		cvResetImageROI( imgcpy_Ptr );
		
		cout << "got here. 2" << endl;
		
		IplImage* hsvImage = HandLocaliser::createHSVImage( imgcpy_Ptr );
		
		IplImage *h_plane_2, *s_plane_2, *v_plane_2;
		IplImage *planes_2[2];
		
		cout << "got here. 3" << endl;
		
		h_plane_2  = cvCreateImage( cvGetSize(hsvImage), 8, 1 );
		s_plane_2  = cvCreateImage( cvGetSize(hsvImage), 8, 1 );
		v_plane_2  = cvCreateImage( cvGetSize(hsvImage), 8, 1 );
		planes_2[0] = h_plane_2;
		planes_2[1] = s_plane_2;
		
		cout << "got here. 4" << endl;
		
		cvShowImage("hsv face image", s_plane_2);
		
		cvWaitKey(0);
		
		cvCvtPixToPlane( hsvFaceImage, h_plane_2, s_plane_2, v_plane_2, 0 );
		
		cvShowImage("hsv face image", hsvFaceImage);
		
		cvWaitKey(0);
		
		//cvShowImage("hsv face image", hsvFaceImage);
		
		cout << "got here. 5" << endl;
		
//		IplImage* dst = cvCreateImage( cvGetSize(hsvImage),8,1);
//		
//		cvZero(dst);
//		
//		cvCalcBackProject(planes_2, //FIX planes[0] instead!?
//						  dst, // Something wrong with the example code!?
//						  hist);
//		
//		cvShowImage( "Back Projection", dst );
//		
//		cvWaitKey(0);
//		
//		//FIX DEALLOCATIONS!
//		
//		
	} else {
		cout << "no face found" << endl;
	}
	
	return 0;

}